package com.jojo.bubble.common.vxInterface;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jojo.bubble.common.enums.VxConfigEnum;
import com.jojo.bubble.utils.HttpInterface;
import org.springframework.util.StringUtils;

public class GetToken {
    private static String Token = null;

    /**
     * @author: jojo
     * @date: 2022/11/11 16:34
     * @desc: 获取token
     */
    public static String getToken() {
        if (StringUtils.isEmpty(Token)) {
            synchronized (GetToken.class) {
                if (StringUtils.isEmpty(Token)) {
                    String url = VxConfigEnum.ROOTURLS.val + "cgi-bin/token?grant_type=client_credential&appid=" + VxConfigEnum.APPID.val + "&secret=" + VxConfigEnum.APPSECRET.val;
                    JSONObject json = JSON.parseObject(HttpInterface.doGet(url));
                    Token = json.getString("access_token");
                    System.out.println(Token);
                }
            }

        } else {
            return Token;
        }

        return Token;
    }

    public static void setTokenNull() {
        Token = null;
    }

    public static void main(String[] args) {
//        String token = UlidCreator.getUlid().toString();
//        System.out.println(token);
        System.out.println(GetToken.getToken());
    }
}
